// pages/sys/role/form/index.js
import {
  request
} from '../../../../request';
import WxValidate from "../../../../utils/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: true,
    form: {
      deptCheckStrictly: true,
      deptIds: [],
      menuCheckStrictly: true,
      menuIds: [],
      remark: null,
      roleKey: null,
      roleName: null,
      roleSort: 0,
      status: "0",
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id != undefined) {
      this.loadFormData(options.id);
    }
    this.initValidate();
  },
  loadFormData(id) {
    request("/system/role/" + id, "GET").then(res => {
      if (res.code == 200) {
        var form = res.data; 
        var sta;
        if(form.status =="0"){
            sta =true;
        }else{
            sta =false;
        }
        form.roleSort = parseInt(form.roleSort); 
        this.setData({
          form: form,
          status:sta
        })
      }
    })
    console.log(this.data)
  },
  initValidate() {
    let rules = {
      roleName: {
        required: true
      },
      roleKey: {
        required: true,
      }
    }
    let message = {
      roleName: {
        required: '请输入角色名'
      },
      roleKey: {
        required: "请输入权限字符"
      },
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  onChange(e) {
    var formData = this.data.form;
    formData.roleSort = e.detail.value;
    console.log(formData);
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  switchStatus() {
    if (this.data.status) {
      this.setData({
        status: false
      })
    } else {
      this.setData({
        status: true
      })
    }
    console.log(this.data.status);
  },
  formSubmit: function (e) {
    console.log(e);
    var formData = this.data.form;
    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      let error = this.WxValidate.errorList[0];
      switch (error.param) {
        case "roleName":
          //TODO
          break;
        case "roleKey":
      }
      console.log(error);
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    if (params.roleName != "") {
      formData["roleName"] = params.roleName;
    }
    if (params.roleKey != "") {
      formData["roleKey"] = params.roleKey;
    }
    if (params.note != "") {
      formData["remark"] = params.note;
    }
    this.data.status == true ? formData["status"] = "0" : formData["status"] = "1";
    var status;
    if (formData.roleId != undefined) {
      status = "PUT";
    } else {
      status = "POST";
    }
    request("/system/role", status, formData).then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '保存成功',
          icon: '',
          duration: 2000,
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
    console.log(formData);
  },
  nameInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.roleName = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  keyInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.roleKey = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  remarkInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.remark = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
})